<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左边 -->
        <div>
          <!-- 图片预览 -->
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 右边 -->
        <div class="spec">
          <!-- 商品信息 -->
          <GoodsName :goods="goods" />
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="getSku" />
          <!-- 数量 -->
          <XtxNumbox label="数量" :max="goods.inventory" v-model="count" />
          <!-- 按钮 -->
          <XtxButton
            type="primary"
            style="margin-top: 20px"
            @click="addToCartBtn"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="$route.params.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
  <div class="loading" v-else></div>
</template>

<script>
import GoodsWarn from './components/goods-warn.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSku from './components/goods-sku'
import GoodsHot from './components/goods-hot.vue'
import { findGoods } from '@/api/product.js'
import { useRoute } from 'vue-router'
import { ref, provide, watch } from 'vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup () {
    const route = useRoute()
    const store = useStore()
    // 接收数据
    const goods = ref(null)

    provide('goods', goods)

    // 时刻监听路由变化 -- bug1
    watch(
      () => route.params.id,
      (id) => {
        goods.value = null
        findGoods(id).then((res) => {
          goods.value = res.result
        })
      },
      { immediate: true }
    )

    const currentSkuId = ref('')
    // 获取子传父过来的skuId
    const getSku = (val) => {
      if (val.skuId) {
        goods.value.inventory = val.inventory
        goods.value.price = val.price
        goods.value.oldPrice = val.oldPrice
        goods.value.attrsText = val.attrsText
        currentSkuId.value = val.skuId
      } else {
        currentSkuId.value = ''
      }
    }
    // 数量
    const count = ref(1)

    // 添加到购物车
    const addToCartBtn = () => {
      // 判断是否选择完整规格
      if (!currentSkuId.value) return Message({ type: 'warn', message: '请选择完整规格' })
      // 判断库存
      if (count.value > goods.value.inventory) return Message({ type: 'warn', message: '库存不足' })
      // 收集信息
      const cartItem = {
        id: goods.value.id,
        skuId: currentSkuId.value,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: goods.value.price,
        nowPrice: goods.value.price,
        count: count.value,
        attrsText: goods.value.attrsText,
        selected: true,
        isEffective: true,
        stock: goods.value.inventory,
        isCollect: false,
        discount: goods.value.discount || 1
      }

      store.dispatch('cart/addCart', cartItem).then((msg) => {
        Message({ type: 'success', message: msg })
      })
    }

    return { goods, count, getSku, addToCartBtn }
  }
}
</script>

<style scoped lang='less'>
.loading {
  min-height: 600px;
  background: #fff url(~@/assets/images/loading.gif) no-repeat center;
}
.goods-info {
  padding: 30px 50px 0;
  min-height: 600px;
  background: #fff;
  display: flex;
  .spec {
    margin-left: 60px;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
